//
//  HandyJSON.swift
//  
//
//  Created by tangbl93 on 2021/1/5.
//

#if canImport(HandyJSON)
import HandyJSON

public extension HandyJSON {
    
    /// deserialize from data
    static func yx_deserialize(from data: Any?) -> Self? {
        
        // [String:Any] or Dictionary
        if let result = data as? [String:Any] {
            return deserialize(from: result)
        }
        
        // String
        if let result = data as? String {
            return deserialize(from: result)
        }
        
        return nil
    }
}

public extension Array where Element: HandyJSON {

    /// deserialize model array from array
    static func yx_deserialize(from data: Any?) -> [Element]? {
        var result:[Element] = []
        guard let dataList = data as? [Any]? else {
            return result
        }
        
        if let elementList = JSONDeserializer<Element>.deserializeModelArrayFrom(array: dataList) {
            for obj in elementList {
                if let obj = obj {
                    result.append(obj)
                }
            }
        }
        
        return result
    }
}
#endif
